﻿/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using Shuriken.Core.Ranking;
using Shuriken.Base;

namespace Shuriken.Core.Store
{
    public class QNStore : IStore
    {
        private string mStoreCategory = string.Empty;
        private string mStoreName = string.Empty;
        private bool mStoreEnabled = false;
        private DateTime mLastUpdated; 
        private IScanner mScanner;

        protected Dictionary<string, IStoreItem> mItemList;

        /// <summary>
        /// Constructor for store
        /// </summary>
        /// <param name="category">Store Category</param>
        /// <param name="name">Store Name</param>
        /// <param name="enabled">Store Enabled</param>
        /// <param name="scanner">For populating the store</param>
        public QNStore(string category, string name, bool enabled, IScanner scanner)
        {
            mStoreCategory = category;
            mStoreName = name;
            mStoreEnabled = enabled; 
            mItemList = new Dictionary<string, IStoreItem>();

            System.Diagnostics.Trace.Assert(scanner != null, "Store " + this.ToString() + " received a null scanner!");
            mScanner = scanner;
        }

        #region IStore Members

        public override string ToString()
        {
            return "qn.store." + this.StoreCategory + "." + this.StoreName;
        }

        /// <summary>
        /// Store Category 
        /// </summary>
        public string StoreCategory
        {
            get { return mStoreCategory; }
        }

        /// <summary>
        /// Store Name 
        /// </summary>
        public string StoreName
        {
            get { return mStoreName; }
        }

        /// <summary>
        /// Store enabled
        /// </summary>
        public bool StoreEnabled
        {
            get
            {
                return mStoreEnabled;
            }
            set
            {
                mStoreEnabled = value;
            }
        }

        /// <summary>
        /// Item count for this store
        /// </summary>
        public int Count
        {
            get { return mItemList.Keys.Count; }
        }

        public Dictionary<string, IStoreItem>.KeyCollection Keys
        {
            get { return mItemList.Keys; }
        }

        public Dictionary<string, IStoreItem>.ValueCollection Values
        {
            get { return mItemList.Values; }
        }
        /// <summary>
        /// Default indexer for store
        /// </summary>
        /// <param name="index">index for an item</param>
        /// <returns>item</returns>
        public IStoreItem this[string index]
        {
            get
            {
                IStoreItem value = null;

                if (mItemList.TryGetValue(index, out value))
                {
                    return value;
                }

                return null;
            }

            set
            {
                mItemList[index] = value;
            }
        }

        /// <summary>
        /// Removes the item from the store 
        /// </summary>
        /// <param name="item">item to remove</param>
        public void RemoveItem(IStoreItem item)
        {
            string index = item.Name;
            mItemList.Remove(index);
        }

        #endregion

        /// <summary>
        /// Shows the time this store has been last updated. 
        /// </summary>
        public DateTime LastUpdated
        {
            get { return mLastUpdated; }
        }

        /// <summary>
        /// Updates the store 
        /// </summary>
        public void UpdateStore()
        {
            mScanner.Update();
            mItemList.Clear();

            foreach(IStoreItem item in mScanner.Items)
            {
                if (item != null)
                {
                    AddStoreItem(item);
                }
            }

            mLastUpdated = DateTime.Now;
        }

        /// <summary>
        /// Adds an item to the store
        /// </summary>
        /// <param name="item">item to add</param>
        /// <returns>the id of the item in the store, -1 if it already exists</returns>
        public int AddStoreItem(IStoreItem item)
        {
            return AddStoreItem(item, item.Name);
        }

        /// <summary>
        /// Adds a Shell32 folder item to the store
        /// </summary>
        /// <param name="item">item to add</param>
        /// <param name="name">name of the item</param>
        /// <returns>the id of the item in the store, -1 if it already exists</returns>
        public int AddStoreItem(IStoreItem item, string name)
        {
            int itemId = -1;

            if (!mItemList.ContainsKey(name))
            {
                item.Store = this; // Adding the store to this, otherwise the app crashes as the store name is baked in 
                                   // at some places. need to think about if it is possible to remove it later. 
                mItemList.Add(name, item);
            }

            return itemId;
        }
    }
}
